package 变量和数据类型;

public class 单例模式 {
    public static void main(String[] args) {
//        GirlFriend instance = GirlFriend.getInstance();
//        GirlFriend instance1 = GirlFriend.getInstance();
//        System.out.println(instance);
//        System.out.println(instance1);
//        System.out.println(GirlFriend.a);
        System.out.println(Cat.getInstance());
        System.out.println(Cat.getInstance());
    }
}
//饿汉式单例模型，有可能对象创建了，但是没有用
class GirlFriend{
    private String name;
    public static int a = 1;
    private static GirlFriend gf = new GirlFriend("小红");

    private GirlFriend(String name){
        System.out.println("world");
        this.name = name;
    }


    public static GirlFriend getInstance(){
        System.out.println("hello");
        return gf;
    }
}

class Cat{
    private String name;
    private static Cat cat;
    private Cat(String name){
        this.name = name;
    }

    public static Cat getInstance(){
        if(cat == null){
            System.out.println("cat 构造器");
            cat = new Cat("喵毛");
        }
        return cat;
    }


    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                '}';
    }
}
